package com.pointlion.mvc.admin.oa.apply.cost;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.pointlion.mvc.admin.oa.workflow.WorkFlowService;
import com.pointlion.mvc.admin.oa.workflow.flowtask.FlowTaskService;
import com.pointlion.mvc.common.base.BaseController;
import com.pointlion.mvc.common.model.OaApplyCost;
import com.pointlion.mvc.common.model.SysOrg;
import com.pointlion.mvc.common.model.SysUser;
import com.pointlion.mvc.common.utils.Constants;
import com.pointlion.mvc.common.utils.DateUtil;
import com.pointlion.mvc.common.utils.StringUtil;
import com.pointlion.mvc.common.utils.UuidUtil;
import com.pointlion.plugin.shiro.ShiroKit;


public class OaApplyCostController extends BaseController {
	public static final OaApplyCostService service = OaApplyCostService.me;
	public static WorkFlowService wfservice = WorkFlowService.me;
	public static FlowTaskService commonFlowService = FlowTaskService.me;
	/***
	 * 列表页面
	 */
	public void getApplyListPage(){
		String type = getPara("type");
		String name = "";
		if("1".equals(type)){
			name = "费用报销申请";
		}else if("2".equals(type)){
			name = "出差费用申请";
		}else if("3".equals(type)){
			name = "业务招待费申请";
		}
		setAttr("type",type);

    	renderIframe("list.html");
    }
	/***
     * 获取分页数据
     **/
    public void listData(){
    	String curr = getPara("pageNumber");
    	String pageSize = getPara("pageSize");
		String endTime = getPara("endTime","");
		String startTime = getPara("startTime","");
		String applyUser = getPara("applyUser","");
    	String type = getPara("type");
    	Page<Record> page = service.getPage(Integer.valueOf(curr),Integer.valueOf(pageSize),type,startTime,endTime,applyUser);
    	renderPage(page.getList(),"",page.getTotalRow());
    }
    /***
     * 保存
     */
    public void save(){
    	OaApplyCost o = getModel(OaApplyCost.class);
    	if(StrKit.notBlank(o.getId())){
    		o.update();
    	}else{
    		o.setId(UuidUtil.getUUID());
    		o.setCreateTime(DateUtil.getCurrentTime());
    		o.save();
    	}
    	renderSuccess();
    }
    /***
     * 获取编辑页面
     */
    public void getEditPage(){
    	String type = getPara("type");
		String name = "";
		if("1".equals(type)){
			name = "费用报销申请";
		}else if("2".equals(type)){
			name = "出差费用申请";
		}else if("3".equals(type)){
			name = "业务招待费申请";
		}
		setAttr("formName",name);
    	//添加和修改
		String view = getPara("view");//查看
		setAttr("view", view);
    	String id = getPara("id");
		OaApplyCost o = new OaApplyCost();
    	if(StrKit.notBlank(id)){//修改
    		o = service.getById(id);
			if("detail".equals(view)){
    			if(StrKit.notBlank(o.getProcInsId())){
    				setAttr("procInsId", o.getProcInsId());
    				setAttr("defId", wfservice.getDefIdByInsId(o.getProcInsId()));
    			}
    		}
    	}else{
    		SysUser user = SysUser.dao.findById(ShiroKit.getUserId());
    		SysOrg org = SysOrg.dao.findById(user.getOrgid());
    		o.setType(type);
    		o.setOrgId(org.getId());
    		o.setOrgName(org.getName());
    		o.setUserid(user.getId());
    		o.setApplyerName(user.getName());
    	}
		setAttr("o", o);
    	setAttr("formModelName",StringUtil.toLowerCaseFirstOne(OaApplyCost.class.getSimpleName()));//模型名称
    	renderIframe("edit.html");
    }
    /***
     * 删除
     * @throws Exception
     */
    public void delete() throws Exception{
		String ids = getPara("ids");
    	//执行删除
		service.deleteByIds(ids);
    	renderSuccess("删除成功!");
    }
    /***
     * 提交
     */
    public void startProcess(){
    	String id = getPara("id");
    	OaApplyCost o = OaApplyCost.dao.getById(id);
    	o.setIfSubmit(Constants.IF_SUBMIT_YES);
    	String insId = wfservice.startProcess(id, o,null,null);
    	o.setProcInsId(insId);
    	o.update();
    	renderSuccess("提交成功");
    }
    /***
     * 撤回
     */
    public void callBack(){
    	String id = getPara("id");
    	try{
    		OaApplyCost o = OaApplyCost.dao.getById(id);
        	wfservice.callBack(o.getProcInsId());
        	o.setIfSubmit(Constants.IF_SUBMIT_NO);
        	o.setProcInsId("");
        	o.update();
    		renderSuccess("撤回成功");
    	}catch(Exception e){
    		e.printStackTrace();
    		renderError("撤回失败");
    	}
    }
}